package com.ddwl.gate.dao;

import com.ddwl.common.dao.TkDao;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.gate.constant.MobileConfigConstant;
import com.ddwl.gate.mapper.MobileRechargeAccountMapper;
import com.ddwl.gate.model.MobileRechargeAccount;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/24 14:39
 */
@Repository
public class MobileRechargeAccountDao extends TkDao<MobileRechargeAccountMapper, MobileRechargeAccount> {

    @Resource
    private MobileRechargeAccountMapper mobileRechargeAccountMapper;

    public void updateAccount(MobileRechargeAccount mra) {
        MobileRechargeAccount entity=this.getByUserIdAndKey();
        entity.setBalance(mra.getBalance());
        entity.setCredit(mra.getCredit());
        this.updateByPrimaryKeySelective(entity);
    }

    public MobileRechargeAccount getByUserIdAndKey(){
        MobileRechargeAccount mra = new MobileRechargeAccount();
        mra.setUserId(MobileConfigConstant.USER_ID);
        mra.setPrivateKey(MobileConfigConstant.KEY);
        MobileRechargeAccount entity=this.selectOne(mra);
        if(null==entity){
            mra.setId(UUIDGenerator.getShortUUID());
            this.insert(mra);
            return mra;
        }
        return entity;
    }

    public List<MobileRechargeAccount> getAll() {
        Example example = new Example(MobileRechargeAccount.class);
        return mobileRechargeAccountMapper.selectByExample(example);
    }

    public void changeId(Map<String, String> map) {
        mobileRechargeAccountMapper.changeId(map);
    }
}
